#!/usr/bin/env ruby -wKU
#encoding: UTF-8

module Utils
  
  class NumberUtils
    
    $cn_2_int_map = { :零 => 0, :一 => 1, :二 => 2, :两 => 2, :三 => 3, :四 => 4,
        :五 => 5, :六 => 6, :七 => 7, :八 => 8, :九 => 9, :十 => 10, :百 => 100, 
        :千 => 1000, :万 => 10000, :亿 => 100000000 }
    
    def self.cn_2_int(cnStr)
      raise ArgumentError.new("nil value") if cnStr.nil?
      
      rtn = 0
      digit = 0
      cnStr.each_char do |x|
        digit = $cn_2_int_map[x.to_sym]
        raise ArgumentError.new("Cannot convert to digit: " + x) if digit.nil?
        rtn = rtn * 10 + digit if digit < 10
      end
      if (digit >= 10)
        rtn *= digit
      end
      rtn
    end
    
  end
end
